package com.student.service;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.student.DBConn;
import com.student.entities.GoodsEntity;

public class GoodsService {

	public static List<GoodsEntity> getGoodsEntity(int n) throws SQLException {
		List<GoodsEntity> goodsList = new ArrayList<GoodsEntity>();
		Connection conn = DBConn.getConn();
		Statement stmt = conn.createStatement();
		String sql = "select * from shop_goods ,shop_seller,shop_goodsclass  where  shop_goods.goods_sellerId = shop_seller.seller_id "
				+ " and shop_goods.goods_classId = shop_goodsclass.class_id  order by goods_saleCount desc limit "
				+ n;

		ResultSet rs = stmt.executeQuery(sql);

		while (rs.next() && n > 0) {
			// System.out.println(rs.getString(1));
			GoodsEntity goods = new GoodsEntity();
			goods.setId(rs.getInt("goods_id"));
			goods.setClassId(rs.getInt("goods_classId"));
			goods.setName(rs.getString("goods_name"));
			goods.setSellerId(rs.getInt("goods_sellerId"));
			goods.setSaleClass(rs.getInt("goods_saleClass"));
			goods.setPrice(rs.getDouble("goods_price"));
			goods.setDescrition(rs.getString("goods_desc"));
			goods.setDiscount(rs.getDouble("goods_discount"));
			goods.setImageUrl(rs.getString("goods_image"));
			goods.setSellerName(rs.getString("seller_name"));
			goods.setRank(rs.getDouble("goods_rank"));
			goods.setClassName(rs.getString("class_name"));
			goods.setSaleCount(rs.getInt("goods_saleCount"));

			goodsList.add(goods);

			n--;
		}
		rs.close();
		stmt.close();
		conn.close();
		return goodsList;
	}

	public static List<GoodsEntity> getSellerGoodsEntity(String id)
			throws SQLException {
		List<GoodsEntity> goodsList = new ArrayList<GoodsEntity>();
		Connection conn = DBConn.getConn();
		Statement stmt = conn.createStatement();
		String sql = "select * from shop_goods,shop_seller where  shop_goods.goods_sellerId = "
				+ id + " and shop_goods.goods_sellerId = shop_seller.seller_id";
		System.out.println(sql);
		ResultSet rs = stmt.executeQuery(sql);

		while (rs.next()) {
			// System.out.println(rs.getString(1));
			GoodsEntity goods = new GoodsEntity();
			goods.setId(rs.getInt("goods_id"));
			goods.setClassId(rs.getInt("goods_classId"));
			goods.setName(rs.getString("goods_name"));
			goods.setSellerId(rs.getInt("goods_sellerId"));
			goods.setSaleClass(rs.getInt("goods_saleClass"));
			goods.setPrice(rs.getDouble("goods_price"));
			goods.setDescrition(rs.getString("goods_desc"));
			goods.setDiscount(rs.getDouble("goods_discount"));
			goods.setImageUrl(rs.getString("goods_image"));
			goods.setSellerName(rs.getString("seller_name"));
			goods.setRank(rs.getDouble("goods_rank"));
			goods.setSaleCount(rs.getInt("goods_saleCount"));

			goodsList.add(goods);
		}
		stmt.close();
		conn.close();
		return goodsList;
	}

	public static List<GoodsEntity> getHotGoods() throws SQLException {
		return getGoodsEntity(10);
	}

	public static List<GoodsEntity> getAllGoods() throws SQLException {
		return getGoodsEntity(Integer.MAX_VALUE);
	}
}
